jQuery("#scores").buttonset();
jQuery("input[name='scores']", jQuery('#scores')).change(

function (e) {
    if (jQuery(this).val() == "1") {
        jQuery("#test2").hide();
        jQuery("#test1").show();
        jQuery("#test3").hide();
    }
    if (jQuery(this).val() == "2") {
        jQuery("#test1").hide();
        jQuery("#test3").hide();
        jQuery("#test2").show();
    }
    if (jQuery(this).val() == "3") {
        jQuery("#test3").show();
        jQuery("#test2").hide();
        jQuery("#test1").hide();
    }
});
(function ($) {
    $(function () {
        /**
         * Experimental Draggable points plugin
         * Revised 2013-02-25 (get latest version from http://jsfiddle.net/highcharts/AyUbx/)
         * Author: Torstein HÃ¸nsi
         * License: MIT License
         *
         */

        (function (Highcharts) {
            var addEvent = Highcharts.addEvent,
                each = Highcharts.each;
            var mySeries;
            /**
             * Filter by dragMin and dragMax
             */

            Highcharts.Chart.prototype.callbacks.push(function (chart) {

                var container = chart.container,
                    dragPoint,
                    dragX,
                    dragY,
                    dragPlotX,
                    dragPlotY;

                chart.redraw(); // kill animation (why was this again?)

                addEvent(container, 'mousedown', function (e) {
                    var hoverPoint = chart.hoverPoint,
                        options;

                    if (hoverPoint) {
                        options = hoverPoint.series.options;
                        if (options.draggableX) {
                            dragPoint = hoverPoint;
                            mySeries = hoverPoint.series;

                            dragX = e.pageX;
                            dragPlotX = dragPoint.plotX;
                        }

                        if (options.draggableY) {
                            dragPoint = hoverPoint;
                            mySeries = hoverPoint.series;

                            dragY = e.offsetY;
                            dragPlotY = dragPoint.plotY + (chart.plotHeight - (dragPoint.yBottom || chart.plotHeight));
                        }
                        if (dragPoint) {
                            if (e.offsetX == undefined) {
                                xpos1 = e.pageX - $(chart.container).offset().left;
                                ypos1 = e.pageY - $(chart.container).offset().top;
                            } else {
                                xpos1 = e.offsetX;
                                ypos1 = e.offsetY;
                            }
                            mousePos = chart.yAxis[0].translate(chart.yAxis[0].height - ypos1 + chart.yAxis[0].top, true);
                            isDragging = true;
                            //console.log("Start "+mousePos);
                            chart.mouseIsDown = false;
                        }
                    }
                });

                addEvent(container, 'mousemove', function (e) {
                    if (isDragging) {
                        if (e.offsetX == undefined) {
                            xpos2 = e.pageX - $(chart.container).offset().left;
                            ypos2 = e.pageY - $(chart.container).offset().top;
                        } else {
                            xpos2 = e.offsetX;
                            ypos2 = e.offsetY;
                        }
                        yVar = chart.yAxis[0].translate(chart.yAxis[0].height - ypos2 + chart.yAxis[0].top, true);
                        mySeries.data[0].update({
                            x: 0,
                            y: yVar
                        });
                        mySeries.data[1].update({
                            x: 299,
                            y: yVar
                        });
                        //mySeries.addPoint([299,yVar],true); 
                        /* mySeries.setData(eval("[]")); 
 mySeries.addPoint([1,yVar],true); 
 mySeries.addPoint([299,yVar],true); 
 console.log(yVar);
/* for(var g=0;g<2;g++) 
 {
 mySeries.points[g].y=yVar;
 }
*/
                        chart.redraw();
                    }
                    //console.log((dragPoint)+" "+(dragPoint.series)+" "+isDragging);
                });

                function drop(e) {
                    isDragging = false;
                    //console.log("End");
                }
                addEvent(document, 'mouseup', drop);
                addEvent(container, 'mouseleave', drop);
            });
        })(Highcharts);
        // End plugin to drag charts //
        jQuery("#slideshow > div:gt(0)").hide();
        setInterval(function () {
            jQuery('#slideshow > div:first')
                .fadeOut(1)
                .next()
                .fadeIn(1)
                .end()
                .appendTo('#slideshow');
        }, 15000);

        jQuery("input[name='pages']", jQuery('#pages')).change(

        function (e) {
            if (jQuery(this).val() == "1") {
                jQuery("#test").show();
                jQuery("#t1est").hide();
                jQuery("#t2est").hide();
            }
            if (jQuery(this).val() == "2") {
                jQuery("#test").hide();
                jQuery("#t1est").show();
                jQuery("#t2est").hide();
            }
            if (jQuery(this).val() == "3") {
                jQuery("#test").hide();
                jQuery("#t1est").hide();
                jQuery("#t2est").show();
            }
        });
        jQuery("#genderslider").slider({
            value: 1,
            min: 11,
            max: 14,
            slide: function (event, ui) {
                jQuery("#gender").html(str[ui.value]);
            }
        });
        jQuery("#personalityslider").slider({
            value: 1,
            min: 1,
            max: 5,
            slide: function (event, ui) {
                jQuery("#personality").html(str[ui.value]);
            }
        });
        jQuery("#personality").val('No choice selected');
        jQuery("#experienceslider").slider({
            value: 6,
            min: 6,
            max: 10,
            slide: function (event, ui) {
                jQuery("#experience").html(str[ui.value]);
            }
        });
        jQuery("#experience").val('No choice selected');
        jQuery("#reachout").buttonset();
        jQuery("#pages").buttonset();
        jQuery("input[name='reachout']", jQuery('#reachout')).change(

        function (e) {
            if (jQuery(this).val() == "1") {
                jQuery("#test1234").hide();
                jQuery("#test12345").show();
                jQuery("#test123456").hide();
            }
            if (jQuery(this).val() == "2") {
                jQuery("#test1234").hide();
                jQuery("#test12345").hide();
                jQuery("#test123456").show();
            }
            if (jQuery(this).val() == "3") {
                jQuery("#test1234").show();
                jQuery("#test12345").hide();
                jQuery("#test123456").hide();
            }
        });
        var str = [];
        str[0] = "I am a Number Cruncher";
        str[1] = "Always liked Maths to Arts";
        str[2] = "Neither here. nor there ";
        str[3] = "Always liked Arts to Maths";
        str[4] = "I am a dreamer";
        str[5] = "Have no Idea";
        str[6] = "I have heard about it";
        str[7] = "I have read a little about it";
        str[8] = "I have used it at times";
        str[9] = "I am a professional";
        str[10] = "Somewhere in between";
        str[11] = "Male";
        str[12] = "Female";
        str[13] = "Not comfortable disclosing";
        var chart, firstChart = true,
            sessionScore = [],
            sessionCount = [],
            sessionAvg = [],
            playerPercentile = [];
        sessionScore[0] = 0, sessionCount[0] = 0, sessionScore[1] = 0, sessionCount[1] = 0, sessionAvg[1] = 0, sessionAvg[1] = 0;
        var a2Ix = [],
            a3Ix = [],
            maxDIx = [],
            minDIx = [],
            lastDIx = [],
            dailyVolIx = [],
            sumPriceIx = [],
            score = [];
        var retrieveIx = 0,
            displayIx = 0,
            minDist = 0;
        var stuck = false,
            isDragging = false,
            singlePoint = true;;
        var pVal = [],
            nVal = [];
        var help;
        var showHelp = true;
        ///// END OF VAIRBALES
        var dataPoints = 300,
            dataPointsInit = 225,
            dataPointsGap = 250,
            playerScore;
        var displayScore = [],
            averageScore = [],
            totalCount = [],
            totalScore = [],
            totalPlayed = [],
            countScore = [],
            percentScore = [];
        var gamePlayingValue = 1;
        displayScore[0] = [];
        displayScore[1] = [];
        displayScore[0][0] = 'Play Game 1';
        displayScore[1][0] = 'Play Game 2';
        displayScore[0][1] = 'Play Game 1';
        displayScore[1][1] = 'Play Game 2';
        displayScore[0][2] = 'Calculating';
        displayScore[1][2] = 'Calculating';
        jQuery("#radioBtnDiv").buttonset();
        $("input[name='view']", $('#radioBtnDiv')).change(

        function (e) {
            if ($(this).val() == "singlePoint") {
                gamePlayingValue = 1;
                singlePoint = true;
                chart.series[3].hide();
                chart.series[4].hide();
                chartUpdate();
                //getDataJson();
                jQuery("#submitChart").hide();
                jQuery("#nextChart").show();
                help = 'Click on the coloured area in the right side to predict the chart position';
                /* finshedImage.hide();
 nextImage.show();
*/
            }
            if ($(this).val() == "tradePoint") {
                gamePlayingValue = 2;
                singlePoint = false;
                chart.series[3].show();
                chart.series[4].show();
                chartUpdate();
                getDataJson();
                jQuery("#submitChart").show();
                jQuery("#nextChart").hide();
                help = 'Drag green line (Take Profit) and red line (Stop Loss). If Green line is seen before red line you make that much of profit and vice versa. If no line is touched, you make profit/loss as per the last position of the chart. Click submit at bottom-right after you have decided the position of lines.';

                /* finshedImage.show();
 nextImage.hide();
*/
            }
        });

        function createChart() {
            chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container2',
                    borderColor: 'black',
                    borderWidth: 3,
                    animation: false,
                    events: {
                        click: function (event) {
                            if (singlePoint) {
                                if (stuck) {
                                    alert("Click on the Next button below to load the next chart");
                                } else if (event.xAxis[0].value > dataPointsGap) {
                                    stuck = true;
                                    pVal[0] = event.xAxis[0].value;
                                    pVal[1] = event.yAxis[0].value;
                                    chart.series[0].addPoint(pVal);
                                    var p = chart.series[0].points[0];
                                    p.update({
                                        marker: {
                                            enabled: true,
                                            symbol: 'circle',
                                            fillColor: "#A0F",
                                            lineColor: "#A0F",
                                            radius: 5
                                        }
                                    });
                                    minDist = 999999999;
                                    minX = 0;
                                    var ohlcIx = 0;
                                    for (var j = dataPointsGap; j < dataPoints; j++) {
                                        xd = (j - pVal[0]) * dailyVolIx[displayIx] / 1.5;
                                        yd = a2Ix[displayIx][j][4] - pVal[1];
                                        dist = Math.sqrt(xd * xd + yd * yd);
                                        if (minDist > dist) {
                                            minDist = dist;
                                            minX = j;
                                            ohlcIx = 4;
                                        }
                                        yd = a2Ix[displayIx][j][3] - pVal[1];
                                        dist = Math.sqrt(xd * xd + yd * yd);
                                        if (minDist > dist) {
                                            minDist = dist;
                                            minX = j;
                                            ohlcIx = 3;
                                        }
                                        yd = a2Ix[displayIx][j][2] - pVal[1];
                                        dist = Math.sqrt(xd * xd + yd * yd);
                                        if (minDist > dist) {
                                            minDist = dist;
                                            minX = j;
                                            ohlcIx = 2;
                                        }
                                        yd = a2Ix[displayIx][j][1] - pVal[1];
                                        dist = Math.sqrt(xd * xd + yd * yd);
                                        if (minDist > dist) {
                                            minDist = dist;
                                            minX = j;
                                            ohlcIx = 1;
                                        }
                                    }
                                    yd = a2Ix[displayIx][minX][1] - pVal[1];
                                    yd = yd * 2 * (maxDIx[displayIx] - minDIx[displayIx]) / (maxDIx[displayIx] + minDIx[displayIx]);
                                    dist = Math.sqrt(xd * xd + yd * yd);
                                    minDist = 10 * minDist / sumPriceIx[displayIx] * dataPoints * (3 - (minX - dataPointsGap) / (dataPoints - dataPointsGap));
                                    score.push(minDist);
                                    sessionCount[0]++;
                                    sessionScore[0] = sessionScore[0] + minDist;
                                    sessionAvg[0] = sessionScore[0] / sessionCount[0];
                                    totalCount[0] = (countScore[0] + sessionCount[0]);
                                    totalScore[0] = (parseInt(averageScore[0]) * countScore[0] + sessionScore[0]) / totalCount[0];
                                    $.ajax({
                                        type: 'GET',
                                        url: '/dataFiles/minuteData/updateMinuteAndGetData.php',
                                        data: {
                                            dataString: minDist.toFixed(2),
                                            typeString: 1
                                        },
        			success: function (dataResponse) {
                    retrieveIx == 14 ? retrieveIx = 1 : retrieveIx++;
                    if (dataResponse.charAt(0) == '[') {
                        originalDataIx = dataResponse;
                        a2Ix[retrieveIx] = $.parseJSON(originalDataIx);
                        a3Ix[retrieveIx] = $.parseJSON(originalDataIx);
                        maxDIx[retrieveIx] = 0, minDIx[retrieveIx] = 9999999, lastDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][dataPointsInit][4]);
                        dailyVolIx[retrieveIx] = 0;
                        sumPriceIx[retrieveIx] = 0;
                        for (var i = 0; i < a2Ix[retrieveIx].length; i++) {
                            if (maxDIx[retrieveIx] < parseFloat(a2Ix[retrieveIx][i][2])) {
                                maxDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][i][2]);
                            }
                            if (minDIx[retrieveIx] > parseFloat(a2Ix[retrieveIx][i][3])) {
                                minDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][i][3]);
                            }
                        }
                        for (var i = 0; i < a2Ix[retrieveIx].length; i++) {
                            a2Ix[retrieveIx][i][1] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][1]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][2] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][2]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][3] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][3]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][4] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][4]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            if (i != 0) dailyVolIx[retrieveIx] = dailyVolIx[retrieveIx] + Math.abs(a2Ix[retrieveIx][i][4] - a2Ix[retrieveIx][i - 1][4]);
                            sumPriceIx[retrieveIx] = sumPriceIx[retrieveIx] + a2Ix[retrieveIx][i][1];
                            if (i > dataPointsInit) {
                                a3Ix[retrieveIx][i][0] = i + 1;
                                a3Ix[retrieveIx][i][1] = "'-'";
                                a3Ix[retrieveIx][i][2] = "'-'";
                                a3Ix[retrieveIx][i][3] = "'-'";
                                a3Ix[retrieveIx][i][4] = "'-'";
                            } else {
                                a3Ix[retrieveIx][i][0] = i + 1;
                                a3Ix[retrieveIx][i][1] = a2Ix[retrieveIx][i][1];
                                a3Ix[retrieveIx][i][2] = a2Ix[retrieveIx][i][2];
                                a3Ix[retrieveIx][i][3] = a2Ix[retrieveIx][i][3];
                                a3Ix[retrieveIx][i][4] = a2Ix[retrieveIx][i][4];
                            }
                        }
                        dailyVolIx[retrieveIx] = dailyVolIx[retrieveIx] / i;
                        if (firstChart == true) {
                            //alert("yipee");
                            firstChart = false;
                            //chart.hideLoading();
                            createChart();
                            chartUpdate();
                        }
                    } else {
                        a2Ix[retrieveIx] = "";
                        a3Ix[retrieveIx] = "";
                        //console.log("Error in data recieved: "+ data);
                    }
                    //console.log("data recieved "+retrieveIx);
                },
                error: function (jqXHR, exception) {
                    //console.log("Error in getting Data. Sorry for the issue. Please come back later.");
                    a2Ix[retrieveIx] = "";
                    a3Ix[retrieveIx] = "";
                }
                                    });
                                    updateDisplayScore(minDist.toFixed(2) + " points", 0, 0);
                                    updateDisplayScore(sessionScore[0].toFixed(2) + " pts " + sessionCount[0] + " plays", 0, 1);
                                    updateDisplayScore(totalScore[0].toFixed(2) + " pts " + totalCount[0] + " plays", 0, 2);
                                    nVal[0] = minX;
                                    nVal[1] = chart.series[1].points[minX].y;
                                    chart.series[0].addPoint(nVal);
                                    chart.series[1].show();
                                    chart.series[2].hide();
                                    chart.yAxis[0].setExtremes(null, null);
                                    pVal = [];
                                    nVal = [];
                                }
                            }
                        }
                    }
                },
                xAxis: {
                    labels: {
                        enabled: false
                    },
                    title: {
                        text: null
                    },
                    plotBands: [{ // mark the weekend
                        color: '#FFF',
                        from: 1,
                        to: dataPointsInit,
                        events: {
                            mouseover: function (event) {
                                $(chart.container).css('cursor', 'default');
                            }
                        }
                    }, { // mark the weekend
                        color: '#e7e7e7',
                        from: dataPointsInit + 1,
                        to: dataPointsGap,
                        events: {
                            mouseover: function (event) {
                                $(chart.container).css('cursor', 'none');
                            }
                        }
                    }, { // mark the weekend
                        color: '#E0BEFF',
                        from: dataPointsGap,
                        to: dataPoints,
                        events: {
                            mousemove: function (event) {
                                $(chart.container).css('cursor', 'default');
                            }
                        }
                    }]
                },
                yAxis: {
                    labels: {
                        enabled: false
                    },
                    title: {
                        text: null
                    }
                },
                plotOptions: {
                    series: {
                        cursor: 'ns-resize',
                        stickyTracking: false,
                        states: {
                            hover: {
                                enabled: false
                            }
                        }
                    }
                },
                legend: {
                    enabled: false,
                    borderWidth: 2,
                    useHTML: true,
                    symbolWidth: 0,
                    symbolPadding: 0,
                    shadow: true,
                    floating: true,
                    align: 'left',
                    verticalAlign: 'top'
                },
                tooltip: {
                    enabled: true,
                    borderWidth: 0,
                    useHTML: true,
                    shared: false,
                    shadow: true,
                    /*positioner: function(boxWidth, boxHeight, point) { 
 return {x:2,y:chart.chartHeight/2}; 
 },*/
                    formatter: function () {
                        true;
                        if (gamePlayingValue == 1) {
                            if (stuck == false) {
                                help = 'Click on the coloured area in the right side to predict the chart position';
                            } else {
                                help = 'Click on the "Next Chart" button at the bottom-right to see the next chart';
                            }
                        } else {
                            if (stuck == false) {
                                help = 'Step 1: Drag green line (Take Profit) and red line (Stop Loss). <br> Step 2: Click submit at bottom-right after you have decided the position of lines.';
                            } else {
                                help = 'Click on the "Next Chart" button at the bottom-right to see the next chart';
                            }
                        }
                        if (showHelp) {
                            return help;
                        } else {
                            return '';
                        }
                    }
                },
                navigator: {
                    enabled: false
                },
                scrollbar: {
                    enabled: false
                },
                rangeSelector: {
                    enabled: false
                },
                title: {
                    text: ''
                },
                series: [{
                    type: 'line',
                    color: 'yellow',
                    showInLegend: false,
                    data: []
                }, {
                    showInLegend: false,
                    type: 'candlestick',
                    animation: false,
                    data: []
                }, {
                    showInLegend: true,
                    type: 'candlestick',
                    animation: false,
                    color: '#CCC',
                    data: []
                }, {
                    showInLegend: false,
                    animation: false,
                    marker: {
                        enabled: false
                    },
                    color: 'green',
                    draggableY: true,
                    data: []
                }, {
                    showInLegend: false,
                    animation: false,
                    marker: {
                        enabled: false
                    },
                    color: 'red',
                    draggableY: true,
                    data: []
                }]
            });
        }
        /*,
 function(chart) { // on complete
 chart.series[2].legendItem.attr('text', 'Good Luck!');
 chart.legend.render(); 
 }
 );*/
        ///// INITATE FUNCTIONS
        getDataJson();
        getDataJson();
        getDataJson();
        getDataJson();
        getDataJson();
        /*getDataJson();getDataJson();getDataJson();getDataJson();getDataJson();*/
        getScore();
        ///// update chart
        function chartUpdate() {
            //alert("entering");
            displayIx == 14 ? displayIx = 1 : displayIx++;
            index = displayIx;
            //console.log(index+" index ");
            //FLAG TOGGLE
            stuck = false;
            if (a2Ix != undefined) {
                //CHART FUNCTIONS
                if (a2Ix[index].length > 10) {
                    TEMPiNDEX = index;
                    chart.series[0].setData(eval("[]"));
                    chart.series[1].setData(eval("[]"));
                    chart.series[2].setData(eval("[]"));
                    if (!singlePoint) {
                        chart.series[3].setData(eval("[]"));
                        chart.series[4].setData(eval("[]"));
                        var pointValY = a2Ix[index][dataPointsInit - 2][4] + 0.10;
                        var pointValY2 = a2Ix[index][dataPointsInit - 2][4] - 0.10;
                        chart.series[3].addPoint([1, pointValY], true);
                        chart.series[3].addPoint([299, pointValY], true);
                        chart.series[4].addPoint([1, pointValY2], true);
                        chart.series[4].addPoint([299, pointValY2], true);
                    }
                    chart.yAxis[0].setExtremes(null, null);
                    var d1 = "[ [" + a2Ix[index].join(" ], [ ") + "] ]";
                    var d2 = "[ [" + a3Ix[index].join(" ], [ ") + "] ]";
                    chart.series[1].setData(eval(d1));
                    chart.series[2].setData(eval(d2));
                    chart.series[1].hide();
                    chart.series[2].show();
                }
            }
            //console.log("TEMPiNDEX "+TEMPiNDEX+"index "+index);
        }
        ///// end of chart update
        ///// get data
        function getDataJson() {
            var originalDataIx = "";
            new Date().toString();
            $.ajax({
                url: '/dataFiles/minuteData/datacsv.php', //the script to call to get data 
                data: "", //you can insert url argumnets here to pass to api.php
                cache: false,
                success: function (data) {
                    retrieveIx == 14 ? retrieveIx = 1 : retrieveIx++;
                    if (data.charAt(0) == '[') {
                        originalDataIx = data;
                        a2Ix[retrieveIx] = $.parseJSON(originalDataIx);
                        a3Ix[retrieveIx] = $.parseJSON(originalDataIx);
                        maxDIx[retrieveIx] = 0, minDIx[retrieveIx] = 9999999, lastDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][dataPointsInit][4]);
                        dailyVolIx[retrieveIx] = 0;
                        sumPriceIx[retrieveIx] = 0;
                        for (var i = 0; i < a2Ix[retrieveIx].length; i++) {
                            if (maxDIx[retrieveIx] < parseFloat(a2Ix[retrieveIx][i][2])) {
                                maxDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][i][2]);
                            }
                            if (minDIx[retrieveIx] > parseFloat(a2Ix[retrieveIx][i][3])) {
                                minDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][i][3]);
                            }
                        }
                        for (var i = 0; i < a2Ix[retrieveIx].length; i++) {
                            a2Ix[retrieveIx][i][1] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][1]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][2] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][2]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][3] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][3]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][4] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][4]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            if (i != 0) dailyVolIx[retrieveIx] = dailyVolIx[retrieveIx] + Math.abs(a2Ix[retrieveIx][i][4] - a2Ix[retrieveIx][i - 1][4]);
                            sumPriceIx[retrieveIx] = sumPriceIx[retrieveIx] + a2Ix[retrieveIx][i][1];
                            if (i > dataPointsInit) {
                                a3Ix[retrieveIx][i][0] = i + 1;
                                a3Ix[retrieveIx][i][1] = "'-'";
                                a3Ix[retrieveIx][i][2] = "'-'";
                                a3Ix[retrieveIx][i][3] = "'-'";
                                a3Ix[retrieveIx][i][4] = "'-'";
                            } else {
                                a3Ix[retrieveIx][i][0] = i + 1;
                                a3Ix[retrieveIx][i][1] = a2Ix[retrieveIx][i][1];
                                a3Ix[retrieveIx][i][2] = a2Ix[retrieveIx][i][2];
                                a3Ix[retrieveIx][i][3] = a2Ix[retrieveIx][i][3];
                                a3Ix[retrieveIx][i][4] = a2Ix[retrieveIx][i][4];
                            }
                        }
                        dailyVolIx[retrieveIx] = dailyVolIx[retrieveIx] / i;
                        if (firstChart == true) {
                            //alert("yipee");
                            firstChart = false;
                            //chart.hideLoading();
                            createChart();
                            chartUpdate();
                        }
                    } else {
                        a2Ix[retrieveIx] = "";
                        a3Ix[retrieveIx] = "";
                        //console.log("Error in data recieved: "+ data);
                    }
                    //console.log("data recieved "+retrieveIx);
                },
                error: function (jqXHR, exception) {
                    //console.log("Error in getting Data. Sorry for the issue. Please come back later.");
                    a2Ix[retrieveIx] = "";
                    a3Ix[retrieveIx] = "";
                }
            });
        }
        /////end of get data
        //// start of get score
        function getScore() {
            $.ajax({
                url: '/dataFiles/minuteData/getUserScore.php', //the script to call to get data 
                data: "", //you can insert url argumnets here to pass to api.php
                cache: false,
                success: function (data) {
                    playerScore = data.split('#');
                    if (playerScore != null) {
                        averageScore[0] = parseFloat(playerScore[0]);
                        countScore[0] = parseFloat(playerScore[1]);
                        percentScore[0] = parseFloat(playerScore[2]);
                        updateDisplayScore(averageScore[0].toFixed(2) + " pts " + countScore[0] + " plays", 0, 2);
                        averageScore[1] = parseFloat(playerScore[3]);
                        countScore[1] = parseFloat(playerScore[4]);
                        percentScore[1] = parseFloat(playerScore[5]);
                        updateDisplayScore(averageScore[1].toFixed(2) + " pts " + countScore[1] + " plays", 1, 2);
                    }
                }
            });
        }
        //// end of get score
        //TOOL BAR FUNCTIONS BELOW NOTHING TO DO WITH CHART
        if (firstChart == true) {
            // chart.showLoading();
        }
        jQuery("#submitChart").button({
            text: true,
            icons: {
                primary: "ui-icon-circle-check"
            }
        })
            .click(function () {
            if (!singlePoint) {
                if (!stuck) {
                    chart.series[1].show();
                    chart.series[2].hide();
                    var p = chart.series[3].points[0].y;
                    var q = chart.series[4].points[0].y;
                    if ((a2Ix[displayIx][dataPointsInit - 1][4] - p) * (a2Ix[displayIx][dataPointsInit - 1][4] - q) > 0) {
                        alert("SL and TP cannot be on same side.");
                        return;
                    }
                    var shortSell = -1;
                    var target = 0;
                    if (p < q) shortSell = 1;
                    if (shortSell == -1) {
                        for (var j = dataPointsInit;
                        ((j < dataPoints) + (target == 0)) == 2; j++) {
                            if (a2Ix[displayIx][j][2] > p) target = 1;
                            if (a2Ix[displayIx][j][3] < q) target = -1;
                        }
                    } else {
                        for (var j = dataPointsInit;
                        ((j < dataPoints) + (target == 0)) == 2; j++) {
                            if (a2Ix[displayIx][j][3] < p) target = 1;
                            if (a2Ix[displayIx][j][2] > q) target = -1;
                        }
                    }
                    if (target == 0) minDist = (a2Ix[displayIx][dataPointsInit - 1][4] - a2Ix[displayIx][dataPoints - 1][4]) * (shortSell);
                    if (target == 1) minDist = Math.abs(p - a2Ix[displayIx][dataPointsInit - 1][4]);
                    if (target == -1) minDist = Math.abs(q - a2Ix[displayIx][dataPointsInit - 1][4]) * (-1);
                    $.ajax({
                        type: 'GET',
                        url: '/dataFiles/minuteData/updateMinuteAndGetData.php',
                        data: {
                            dataString: minDist.toFixed(2),
                            typeString: 2
                        },
					success: function (dataResponse) {
                    retrieveIx == 14 ? retrieveIx = 1 : retrieveIx++;
                    if (dataResponse.charAt(0) == '[') {
                        originalDataIx = dataResponse;
                        a2Ix[retrieveIx] = $.parseJSON(originalDataIx);
                        a3Ix[retrieveIx] = $.parseJSON(originalDataIx);
                        maxDIx[retrieveIx] = 0, minDIx[retrieveIx] = 9999999, lastDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][dataPointsInit][4]);
                        dailyVolIx[retrieveIx] = 0;
                        sumPriceIx[retrieveIx] = 0;
                        for (var i = 0; i < a2Ix[retrieveIx].length; i++) {
                            if (maxDIx[retrieveIx] < parseFloat(a2Ix[retrieveIx][i][2])) {
                                maxDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][i][2]);
                            }
                            if (minDIx[retrieveIx] > parseFloat(a2Ix[retrieveIx][i][3])) {
                                minDIx[retrieveIx] = parseFloat(a2Ix[retrieveIx][i][3]);
                            }
                        }
                        for (var i = 0; i < a2Ix[retrieveIx].length; i++) {
                            a2Ix[retrieveIx][i][1] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][1]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][2] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][2]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][3] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][3]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            a2Ix[retrieveIx][i][4] = (maxDIx[retrieveIx] - a2Ix[retrieveIx][i][4]) / (maxDIx[retrieveIx] - minDIx[retrieveIx]);
                            if (i != 0) dailyVolIx[retrieveIx] = dailyVolIx[retrieveIx] + Math.abs(a2Ix[retrieveIx][i][4] - a2Ix[retrieveIx][i - 1][4]);
                            sumPriceIx[retrieveIx] = sumPriceIx[retrieveIx] + a2Ix[retrieveIx][i][1];
                            if (i > dataPointsInit) {
                                a3Ix[retrieveIx][i][0] = i + 1;
                                a3Ix[retrieveIx][i][1] = "'-'";
                                a3Ix[retrieveIx][i][2] = "'-'";
                                a3Ix[retrieveIx][i][3] = "'-'";
                                a3Ix[retrieveIx][i][4] = "'-'";
                            } else {
                                a3Ix[retrieveIx][i][0] = i + 1;
                                a3Ix[retrieveIx][i][1] = a2Ix[retrieveIx][i][1];
                                a3Ix[retrieveIx][i][2] = a2Ix[retrieveIx][i][2];
                                a3Ix[retrieveIx][i][3] = a2Ix[retrieveIx][i][3];
                                a3Ix[retrieveIx][i][4] = a2Ix[retrieveIx][i][4];
                            }
                        }
                        dailyVolIx[retrieveIx] = dailyVolIx[retrieveIx] / i;
                        if (firstChart == true) {
                            //alert("yipee");
                            firstChart = false;
                            //chart.hideLoading();
                            createChart();
                            chartUpdate();
                        }
                    } else {
                        a2Ix[retrieveIx] = "";
                        a3Ix[retrieveIx] = "";
                        //console.log("Error in data recieved: "+ data);
                    }
                    //console.log("data recieved "+retrieveIx);
                },
                error: function (jqXHR, exception) {
                    //console.log("Error in getting Data. Sorry for the issue. Please come back later.");
                    a2Ix[retrieveIx] = "";
                    a3Ix[retrieveIx] = "";
                }
                    });
                    score.push(minDist);
                    sessionCount[1]++;
                    sessionScore[1] = sessionScore[1] + minDist;
                    sessionAvg[1] = sessionScore[1] / sessionCount[1];
                    totalCount[1] = (countScore[1] + sessionCount[1]);
                    totalScore[1] = (parseInt(averageScore[1]) * countScore[1] + sessionScore[1]) / totalCount[1];
                    updateDisplayScore(minDist.toFixed(2) + " points", 1, 0);
                    updateDisplayScore(sessionScore[1].toFixed(2) + " pts " + sessionCount[1] + " plays", 1, 1);
                    updateDisplayScore(totalScore[1].toFixed(2) + " pts " + totalCount[1] + " plays", 1, 2);
                    stuck = true;
                    jQuery("#submitChart").hide();
                    jQuery("#nextChart").show();
                    /* nextImage.show();
 finshedImage.hide();
*/
                }
            }
        });
        jQuery("#submitChart").hide();
        jQuery("#scrollUp").button({
            text: false,
            icons: {
                primary: "ui-icon-circle-triangle-n"
            }
        })
            .click(function () {
            var yData = chart.yAxis[0].getExtremes();
            chart.yAxis[0].setExtremes((yData.min + chart.yAxis[0].tickInterval), (yData.max + chart.yAxis[0].tickInterval));
        });
        jQuery("#scrollDn").button({
            text: false,
            icons: {
                primary: "ui-icon-circle-triangle-s"
            }
        })
            .on('click', function () {
            var yData = chart.yAxis[0].getExtremes();
            chart.yAxis[0].setExtremes((yData.min - chart.yAxis[0].tickInterval), (yData.max - chart.yAxis[0].tickInterval));
        });
        jQuery("#nextChart").button({
            text: true,
            icons: {
                primary: "ui-icon-circle-arrow-e"
            }
        })
            .click(function () {
            if (stuck) {
                chart.tooltip.hide();
                while (((displayIx >= retrieveIx) + retrieveIx != 1) == 2) {
                    alert("Please click again as either your network is currently slow or our servers are facing heavy load");
                }
                chartUpdate();
                getDataJson();
                if (!singlePoint) {
                    jQuery("#submitChart").show();
                    jQuery("#nextChart").hide();
                    /* finshedImage.show();
 nextImage.hide();
*/
                }
                stuck = false;
            }
        });
        jQuery("#scoreHead").button();
        jQuery("#scoreText").button();
        jQuery("#contactus").button({
            text: false,
            icons: {
                primary: "ui-icon-mail-closed"
            }
        });
        jQuery("#logoutin").button({
            text: false,
            icons: {
                primary: "ui-icon-locked"
            }
        });
        jQuery("#facebook").button({
            text: false,
            icons: {
                primary: "sy-icon-fb"
            }
        });
        jQuery("#playhelp").button({
            text: false,
            icons: {
                primary: "ui-icon-help"
            }
        })
            .click(function () {
            showHelp = !showHelp;
            if (!showHelp) jQuery(".highcharts-tooltip").css('display', 'none');
            if (showHelp) jQuery(".highcharts-tooltip").css('display', 'block');
        });
        jQuery("#scoreHead")[0].gameChoice = 0;
        jQuery("#scoreHead")[0].tenorChoice = 0;
        jQuery("#scoreHead").button()
            .click(function () {
            var options;
            if ($(this).text() === "One: Predict") {
                $(this).html('<span class="ui-button-text">Two: Trade</span>');
                $(this)[0].gameChoice = 1;
                updateDisplayForm(parseInt(jQuery("#scoreHead")[0].gameChoice), jQuery("#scoreHead")[0].tenorChoice);
            } else {
                $(this).html('<span class="ui-button-text">One: Predict</span>');
                $(this)[0].gameChoice = 0;
                updateDisplayForm(parseInt(jQuery("#scoreHead")[0].gameChoice), jQuery("#scoreHead")[0].tenorChoice);
            }
        });
        jQuery("#next").button({
            text: false,
            icons: {
                primary: "ui-icon-play"
            }
        });
        jQuery("#repeat").buttonset();
        jQuery("#repeat0")
            .click(function () {
            jQuery("#scoreHead")[0].tenorChoice = 0;
            updateDisplayForm(parseInt(jQuery("#scoreHead")[0].gameChoice), 0);
        });
        jQuery("#repeat1")
            .click(function () {
            jQuery("#scoreHead")[0].tenorChoice = 1;
            updateDisplayForm(parseInt(jQuery("#scoreHead")[0].gameChoice), 1);
        });
        jQuery("#repeatall")
            .click(function () {
            jQuery("#scoreHead")[0].tenorChoice = 2;
            updateDisplayForm(parseInt(jQuery("#scoreHead")[0].gameChoice), 2);
        });

        function updateDisplayScore(a, b, c) {
            displayScore[b][c] = a;
            if (parseInt(jQuery("#scoreHead")[0].gameChoice) == b) {
                if (jQuery("#scoreHead")[0].tenorChoice == (c)) {
                    updateDisplayForm(b, c);
                }
            }
        }

        function updateDisplayForm(b, c) {
            jQuery("#scoreText").html('<span class="ui-button-text">' + displayScore[b][c] + '</span>');
        }
        ///////
    });
})(jQuery);
jQuery(document).ready(function () {
    jQuery(".sy-header").equalHeights();
    jQuery(function () {
        jQuery(".slideshow").show();
    });
    jQuery('.show_hide').showHide({
        speed: 1000, // speed you want the toggle to happen 
        easing: '', // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
        changeText: 1, // if you dont want the button text to change, set this to 0
        showText: 'Click here to view more information', // the button text to show when a div is closed
        hideText: 'Click here to view less information' // the button text to show when a div is open

    });
});
(function ($) {
    $.fn.showHide = function (options) {
        var defaults = {
            speed: 1000,
            easing: '',
            changeText: 0,
            showText: 'Show',
            hideText: 'Hide'
        };
        var options = $.extend(defaults, options);
        $(this).click(function () {
            $('.toggleDiv').slideUp(options.speed, options.easing);
            var toggleClick = $(this);
            var toggleDiv = $(this).attr('rel');
            $(toggleDiv).slideToggle(options.speed, options.easing, function () {
                if (options.changeText == 1) {
                    $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
                }
            });
            return false;
        });
    };
})(jQuery);